package com.sun.hunk.hibernate.dao;

import com.sun.hunk.hibernate.BaseHibernateDAO;
import com.sun.hunk.hibernate.domain.GenericCode;
import com.sun.hunk.hibernate.domain.impl.GenericCodeImpl;
import com.sun.hunk.util.CommonUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author sunil
 */
public class GenericCodeDAO extends BaseHibernateDAO<GenericCodeImpl> {

    public GenericCodeDAO() {
        super(GenericCodeImpl.class);
    }

    public enum Code {

        OP,
        AR_LCLE("AR-LCLE"),
        AC,
        AR;
        String value;

        private Code() {
        }

        private Code(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            if (CommonUtil.isEmpty(value)) {
                return super.toString();
            } else {
                return value;
            }
        }
    }

    public enum GenericCodeType {

        CHARGE_CODE(2),
        COMMODITY_CODE(4),
        COMMODITY_DESC(4),
        STATE(10),
        TYPE_OF_MOVE(48),
        FREQUENCY_OF_SERVICE(50),
        TRANSPORTATION_MODE(49),
        CURRENCY(32),
        COST_CODE(36),
        COST_TYPE(37),
        VESSEL(14),
        EQUIPMENT_TYPE(59),
        CLIENT_TYPE(60),
        TRANSACTION_STATUS(61),
        SUB_LEDGER(62),
        TRANSACTION_TYPE(63),
        SHIPMENT_TYPE(64);
        private int id;

        private GenericCodeType(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }
    }

    public GenericCode findById(Long id) {
        return super.findById(id);
    }

    public GenericCode getGenericCode(GenericCodeType type, Code code) {
        return getGenericCode(type, code.toString());
    }

    public GenericCode getGenericCode(GenericCodeType type, String code) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("codeType", type.getId());
        map.put("code", code);
        List<GenericCode> list = find(map);
        if (CommonUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        new GenericCodeDAO().getCurrentSession().beginTransaction();
        new GenericCodeDAO().getGenericCode(GenericCodeType.TRANSACTION_STATUS, GenericCodeDAO.Code.OP);

    }
}
